package com.maben.million_insert.service.impl;

import com.maben.million_insert.mapper.CirRootMapper;
import com.maben.million_insert.pojo.CirRootPO;
import com.maben.million_insert.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 异步业务实现类
 */
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    @Resource
    private CirRootMapper cirRootMapper;
    @Override
    @Async("asyncServiceExecutor")
    public void executeAsync(List<CirRootPO> cirRootPOList, CountDownLatch countDownLatch)throws Exception {
        try{
            log.warn("start executeAsync");
            //异步线程要做的事情
            cirRootMapper.saveCirRootBatch(cirRootPOList);
            log.warn("end executeAsync");
        }finally {
            // 很关键, 无论上面程序是否异常必须执行countDown,否则await无法释放
            countDownLatch.countDown();
        }
    }
}
